﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using IBupro.Models.Classes;
using iBuproModel;
using System.Web.Mvc;
using iBuproModel.Repositories.Interfaces;

namespace IBupro.Models.Lector
{
    public class EnrollLectorToSubjectModel : CustomModel
    {
        public User Lector { get; set; }
        public IEnumerable<int> SelectedSubjects {get;set; }
        public List<string> ActualSubjects { get; set; }

        public MultiSelectList GetSubjectsSelectList(string departmentPrincipal, string[] actualSubjects)
        {
            IUserRepository users = repositoryService.UserRepositoryCreateInstance();
            User principal = users.GetUserByUserName(departmentPrincipal);

            if (principal == null)
                throw new Exception("User not found");

            if (!users.IsUserInRole(principal, "Principal"))
                throw new Exception("User " + principal.Username + " is not in Principal role.");

            var list =
                from s in
                    principal.DepartmentPrincipalDepartments.First().Subjects.Where(s => s.Visible == true).ToList()
                       select new
                       {
                           Id = s.Id,
                           Name = s.Shortcut + " " + s.Name
                       };
            return new MultiSelectList(list, "Id", "Name", actualSubjects);
            
        }

        public MultiSelectList GetSubjectsSelectList(string departmentPrincipal)
        {
            return GetSubjectsSelectList(departmentPrincipal, null);
        }
    }
}